﻿/// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System.Linq;
using System.Net;
using Microsoft.ServiceModel.Web;
using Microsoft.ServiceModel.Web.SpecializedServices;

using Intergr8.GoogleVisualization.DataFactory;
using Intergr8.GoogleVisualization.Interfaces;

using DataServicesJSONP;

// The following line sets the default namespace for DataContract serialized typed to be ""
[assembly: ContractNamespace("", ClrNamespace = "WCFDataService")]

namespace WCFDataService
{
   // [ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
   // [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [JSONPSupportBehavior]
    public class Service : SingletonServiceBase<DataResponse>, IDataService<DataResponse>
    {


        /// <summary>
        /// This method is invoked for HTTP Get requests specifying a particular Data Source name
        /// </summary>
        /// <remarks>
        /// <para>GetData is a common method to parse the request, retrieve the data and respond with a valid DataContract compliant object.</remarks>
        /// <returns>DataResponse object populated with data and attributes as requested.</para>
        /// <para>This work is performed by the VisualizationDataRequestFactory. The factory class contains methods for invoking the parsing and data retrieval functionality.</para>
        /// <para>Conversion of the ResponseMessage into a calid DataContract compliant object is performed by the WCFDataResponseFactory.  </para></returns>
        public DataResponse GetData(string datasourcename, string tq, string tqx)
        {
            // Capture the request query and paramters into a RequestHandler object
            // The RequestHandler interprets the Google Visualization parameters and query ready for use in generating the response.
            IRequestHandler request =  VisualizationDataRequestFactory.CaptureRequest(tq, tqx);

            // Using the requested datasource and the RequestHandler, the datasource is queried and processed according
            // to the RequestHandlers state.  The resultant ResponseMessage contains the attributes and data ready to return to the client
            IResponseMessage responseMessage = VisualizationDataRequestFactory.CreateResponse(request, datasourcename);

            // Finally the responseMessage is transformed into the DataContract structure represented by a DataResponse object.
            DataResponse responseData = WCFDataResponseFactory.CreateDataResponse(responseMessage);

            // Return the DataResponse object to the calling method
            return responseData;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="datasourcename"></param>
        /// <returns></returns>
        //public DataResponse GetDefaultDataXML(string datasourcename)
        //{
        //    return GetData(datasourcename, null,null);
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="datasourcename"></param>
        /// <param name="tq"></param>
        /// <param name="tqx"></param>
        /// <returns></returns>
        //public DataResponse GetFullDataXML(string datasourcename, string tq, string tqx)
        //{
        //    return GetData(datasourcename, tq, tqx);
        //}

        public DataResponse GetDataJSON(string datasourcename, string tq, string tqx)
        {
            return GetData(datasourcename, tq, tqx);
        }



        #region To Be Deleted

        /// <summary>
        /// This method is invoked for HTTP GET requests.
        /// A null return value will result in a response status code of NotFound (404), unless the method explicitly sets the status code to a different error.
        /// </summary>
        /// <returns>The resource if it exists, null otherwise.</returns>
        protected override DataResponse OnGetItem()
        {
            throw new WebProtocolException(HttpStatusCode.NotImplemented);
        }

        /// <summary>
        /// This method is invoked for HTTP POST requests.
        /// If the resource does not exist, create the resource and set wasResourceCreated to true.
        /// If the resource already exists, then the semantics of the Post call is application-specific.
        /// A null return value will result in a response status code of Conflict (409), unless the method explicitly sets the status code to a different error
        /// </summary>
        /// <param name="newValue"></param>
        /// <param name="wasResourceCreated"></param>
        /// <returns>The item that was Posted. Returns null if the Post could not be processed due to the current state of the resource.</returns>
        protected override DataResponse OnAddItem(DataResponse newValue, out bool wasResourceCreated)
        {
            // Not required in this service version
            wasResourceCreated = false;

            throw new WebProtocolException(HttpStatusCode.NotImplemented);
        }

        /// <summary>
        /// This method is invoked for HTTP PUT requests. This method should be idempotent.
        /// If the resource does not exist, create the resource and set wasResourceCreated to true.
        /// If the resource already exists, update the resource to the new value.
        /// A null return value will result in a response status code of InternalServerError (500), unless the method explicitly sets the status code to a different error
        /// </summary>
        /// <param name="newValue">The new item to be created, or to replace the old item.</param>
        /// <param name="wasResourceCreated">Indicates if the item was created.</param>
        /// <returns>The new item created or used to replace. Returns null in case of an error.</returns>
        protected override DataResponse OnUpdateItem(DataResponse newValue, out bool wasResourceCreated)
        {
            // Not required in this service version
            wasResourceCreated = false;

            throw new WebProtocolException(HttpStatusCode.NotImplemented);

            return null;
        }

        /// <summary>
        /// This method is called for HTTP DELETE requests
        /// Delete the resource, if it exists. If the resource does not exist return false. 
        /// A return value of false will result in a response status code of NotFound (404), unless the method explicitly sets the status code to a different error 
        /// </summary>
        /// <returns>False if delete failed, true if successful.</returns>
        protected override bool OnDeleteItem()
        {
            throw new WebProtocolException(HttpStatusCode.NotImplemented);
        }

        #endregion

    }


}